<?php
class Model_Papers extends Model {

	protected $tbPrefix = "supe_";
	protected $tbName = "paperitems";
	protected $tbHas = "papermessage";
	
	function findByAuthor($author){
		$tbname_item = $this->tbName($this->tbName);
		$tbname_message = $this->tbName($this->tbHas);
		return $this->selectBySql("select subject,i.itemid from $tbname_message m left join $tbname_item i on m.itemid = i.itemid  where paperauthor like '%$author%' order by dateline desc");
	}
	/**
	 * 返回作者包含专家名称的论文数量
	 *
	 * @param string $author
	 * @return int
	 */
	function getExpertPaperQty($author){
		$tbname_message = $this->tbName($this->tbHas);
		$data = $this->selectBySql("select count(*) from $tbname_message where paperauthor like '%$author%' ");
		return intval(current($data[0]));
	}
	
	function getListByCondition($conditionString){
		$tbname_item = $this->tbName($this->tbName);
		$tbname_message = $this->tbName($this->tbHas);
		
		$pars = parse_pars($conditionString);
	
		$condition = array();
		$data = array();
	
		if($pars['paperauthor']){
			$condition[] = "m.paperauthor = '{$pars[paperauthor]}'";
		}
	
		if($pars['catebig']){
			$condition[] = "m.catebig like '%{$pars[catebig]}%'";
		}
		if($pars['catid']){
			$condition[] = "i.catid in ({$pars[catid]})";
		}
		if($pars['grade']){
			$condition[] = "i.grade in ({$pars[grade]})";
		}
	
		if($pars['dateline']){
			$condition[] = "unix_timestamp() - i.dateline  < ({$pars[dateline]}) ";
		}
		if($pars['meeting']){
			$condition[] = "m.meeting='$pars[meeting]'" ;
		}
	
		if(count($condition)>0){
			$where =  ' and  ' . join(' and ', $condition);
		}
		if($pars['order']){
			$where .= " order by $pars[order] ";
		} else {
			$where .= " order by dateline desc ";
		}
	

		$sql = "SELECT count(*) FROM $tbname_item i 
				left join $tbname_message m
				on i.itemid = m.itemid
				WHERE 1 $where";
		
		//$pars['perpage']必须先于$pars['limit']的条件处理
		if($pars['perpage']){
			
			$result =  $this->selectBySql($sql);
			$total = current($result[0]);
			$pager = new Pagination($total,$pars['perpage'],_gp('page','int'));
			$pars['limit'] = "{$pager->minimum} , {$pager->pagesize}";
			$data['pager'] = $pager->display;
		} 
		
		//$pars['limit']必须后于$pars['perpage']的条件处理
		if($pars['limit']){
			$where .= " limit $pars[limit] ";
		}
	
		$sql = "SELECT * FROM $tbname_item i 
				left join $tbname_message m
				on i.itemid = m.itemid
				WHERE 1 $where";
		
		$list = $this->selectBySql($sql);
		
		foreach ($list as &$row){
			$row['subjectall'] = $row['subject'];
			if($pars['subjectlen']){
				$row['subject'] = _cutstr($row['subject'],$pars['subjectlen'],$pars['subjectdot']);
				if(!$pars['subjectdot'] && (substr($row['subject'], -1) == '.')){
					$row['subject'] = substr($row['subject'], 0, -1);
				}
			}
		}
		
		if($pars['perpage'] == ""){
			$data = $list;
		} else {
			$data['list'] = $list;
		}
		return $data;
	}
	
}
?>